<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%category}}".
 *
 * @property string $cateid
 * @property string $title
 * @property string $parentid
 * @property string $createtime
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['parentid', 'required','message'=>'上级分类不能为空'],
            [['title'], 'required', 'message' => '标题名称不能为空'],
            ['createtime','safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'cateid' => 'Cateid',
            'title' => '分类名称',
            'parentid' => '上级分类',
            'createtime' => 'Createtime',
        ];
    }
    
    public function add($data)
    {
        $data['Category']['createtime']= time();
        if($this->load($data) && $this->save()){
            return true;
        }
        return false;
    }
    
    public function getData()
    {
        $cates= self::find()->asArray()->all();
        return $cates;
    }
    public function getTree($cates,$pid=0)
    {
        $tree=[];
        foreach ($cates as $cate){
            if($cate['parentid']==$pid){
                $tree[]=$cate;
                //把两个数组合并为一个数组：array_merge
                $tree= array_merge($tree, $this->getTree($cates, $cate['cateid']));
            }
        }
        return $tree;
    }
    
    //设置分类前缀,找到分类的级别，然后为此分类添加相应的前缀
    public function setPrefix($data,$p="|----")
    {
        $tree=[];
        $num=1;
        $prefix=[0=>1];
        while($val= current($data)){
            $key= key($data);
            if($key>0){
                if($data[$key-1]['parentid']!=$val['parentid']){
                    $num++;
                }
            }
            if(array_key_exists($val['parentid'], $prefix)){
                $num=$prefix[$val['parentid']];
            }
            $val['title']= str_repeat($p, $num).$val['title'];
            $prefix[$val['parentid']]=$num;
            $tree[]=$val;
            next($data);
        }
        return $tree;
    }
    
    //获取分类中所对应的值
    public function getOptions()
    {
        $data= $this->getData();
        $treeBefore= $this->getTree($data);
        $tree= $this->setPrefix($treeBefore);
        $list=['添加顶级分类'];
//        var_dump($tree);exit;
        foreach ($tree as $cate){
            $list[$cate['cateid']]=$cate['title'];
        }
        return $list;
    }
    
    public function getTreeList()
    {
        $data= $this->getData();
        $treeBefore= $this->getTree($data);
        $tree= $this->setPrefix($treeBefore);
        return $tree;
    }
    
    //获取顶级分类和二级分类
    public static function getMenu()
    {
        $top= self::find()->where('parentid=:pid',[':pid'=>'0'])->asArray()->all();
        $data=[];
        foreach ($top as $k=>$cate){
            $cate['children']= self::find()->where('parentid=:id',[':id'=>$cate['cateid']])->asArray()->all();
            $data[$k]=$cate;
        }
        return $data;
    }
    
    //获取三级分类
    public static function getMenuThreeLevel()
    {
        $top= self::find()->where('parentid=:pid',[':pid'=>'0'])->asArray()->all();
        $data=[];
        foreach ($top as $k=>$cate){
            $cate['children']= self::find()->where('parentid=:id',[':id'=>$cate['cateid']])->asArray()->all();
            foreach ($cate['children'] as $three=>$catethree){
                $catethree['grandchildren']= self::find()->where('parentid=:id',[':id'=>$catethree['cateid']])->asArray()->all();
                $cate['children'][$three]=$catethree;
            }
            $data[$k]=$cate;
        }
        return $data;
    }

}
